package com.xinhe.web.controller;
import com.querydsl.core.QueryResults;
import com.xinhe.web.annotation.WebUsed;
import com.xinhe.web.common.Result;
import com.xinhe.web.entity.Image;
import com.xinhe.web.service.AliyunService;
import com.xinhe.web.util.ValidatorUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

@Api(value = "阿里中心", tags = {"阿里中心"})
@RestController
@RequestMapping(value = {"/alibaba"}, produces = {"application/json;charset=UTF-8"})

public class AliyunController {
    @Autowired
    private AliyunService aliyunService;
    @PostMapping(value = "/uploadPhoto")
    public Result uploadPhoto(@RequestParam("file") MultipartFile file) {
        return aliyunService.uploadPhoto(file);
    }
    @RequestMapping(value="/v2/sendVerifyCodeByMobile")
    public void sendVerifyCodeByMobileV2(@RequestParam String mobile, @RequestParam(defaultValue = "86") String timeZone) {
        if ("86".equals(timeZone)){
            if (mobile.length() !=11){
                throw new RuntimeException("手机格式不正确，请重新输入");
            }
            if (!ValidatorUtil.isMobileChina(mobile)){
                throw new RuntimeException("手机格式不正确，请重新输入");
            }
        }
        aliyunService.sendCodeByMobileV(mobile, timeZone);
    }

    @WebUsed
    @ApiOperation(httpMethod = "POST", value = "添加轮播图")
    @PostMapping(value = "/saveImageText")
    public Result saveImageText(@RequestParam("file") MultipartFile file,String type,Integer sequence) {
        return aliyunService.saveImageText(file,type,sequence);
    }
    @WebUsed
    @ApiOperation(httpMethod = "GET", value = "分页查询图片")
    @RequestMapping({"/pageImageText"})
    public QueryResults<Image> pageImageText(String type, int pageNum, int pageSize) {
        return aliyunService.pageImageText(type,pageNum,pageSize);
    }
    @WebUsed
    @ApiOperation(httpMethod = "GET", value = "查询图片详情")
    @RequestMapping({"/detailImageText"})
    public Result detailImageText(Long id) {
        return aliyunService.detailImageText(id);
    }

    @WebUsed
    @ApiOperation(httpMethod = "DELETE", value = "删除图片详情")
    @RequestMapping({"/deleteImageText"})
    public Result deleteImageText(Long id) {
        return aliyunService.deleteImageText(id);
    }


}
